// T1.  Distance from School and Preprimary Enrollment
// T1.  Distance from School and Preprimary Enrollment
// T1.  Distance from School and Preprimary Enrollment

// PRELIMINARIES

clear
set more off
set scheme s1mono


// LOAD CLEAN(ISH) BASELINE DATA FROM PHASE II
	
use "JakielaOzierFernaldKnauer-preschool-data.dta", replace


// FIRST STAGE

** OLS:  table with 4 columns for age in years, panel a w/o controls, panel b w/ them


** OLS without controls
forvalues i = 3/6 {
	reg inschool schdist if roundage==`i', cluster(respid)
	mat V = r(table)
	local _beta`i' = string(V[1,1],"%04.3f")
	local _se`i' = string(V[2,1],"%04.3f")
	local _pval`i' = string(V[4,1],"%04.3f")
	if V[4,1] < 0.001 {
		local _pval`i' = "p$<$0.001"
	}
	local _obs`i' = e(N)
}
cap file close fh
file open fh using "out/T1-firststage1.tex", write replace

file write fh "Distance to school (km) & `_beta3' & `_beta4' & `_beta5' & `_beta6'  \\  " _newline
file write fh "		    & (`_se3') & (`_se4') & (`_se5') & (`_se6')  \\  " _newline
file write fh "		    & [`_pval3'] & [`_pval4'] & [`_pval5'] & [`_pval6']  \\  " _newline

file close fh

** OLS with controls

local controls "male haz cgmom momluo momedu hhsize sibs numassets dadabsent dadeduc dadluo missing_data i.childage haz_imputed i.schoolid"
forvalues i = 3/6 {
	reg inschool schdist `controls' if roundage==`i', cluster(respid)
	mat V = r(table)
	local _beta`i' = string(V[1,1],"%04.3f")
	local _se`i' = string(V[2,1],"%04.3f")
	local _pval`i' = string(V[4,1],"%04.3f")
	if V[4,1] < 0.001 {
		local _pval`i' = "p$<$0.001"
	}
}

cap file close fh
file open fh using "out/T1-firststage2.tex", write replace

file write fh "Distance to school (km) & `_beta3' & `_beta4' & `_beta5' & `_beta6'  \\ " _newline
file write fh "		    & (`_se3') & (`_se4') & (`_se5') & (`_se6')  \\ " _newline
file write fh "		    & [`_pval3'] & [`_pval4'] & [`_pval5'] & [`_pval6']  \\ " _newline

file close fh

cap file close fh
file open fh using "out/T1-firststageinfo.tex", write replace

file write fh "Obs. & `_obs3' & `_obs4' & `_obs5' & `_obs6'  \\  " _newline

file close fh
